import 'package:huxiaodui/api/api.dart';
import 'package:huxiaodui/components/code_input.dart';
import 'package:huxiaodui/view/pop_pages/register_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class LoginPage extends StatelessWidget {
  const LoginPage({super.key});

  @override
  Widget build(BuildContext context) {
    TextEditingController accountController = TextEditingController();
    TextEditingController passwordController = TextEditingController();

    void login() {
      if (accountController.text == "") {
        EasyLoading.showError("请输入账号");
        return;
      }
      if (passwordController.text == "") {
        EasyLoading.showError("请输入密码");
        return;
      }

      Api.mobileApp.login(
        account: accountController.text,
        password: passwordController.text,
      );
    }

    Widget image1 = Container(
      decoration: const BoxDecoration(
          image:
              DecorationImage(image: AssetImage('assets/images/light-1.png'))),
    );
    Widget image2 = Container(
      decoration: const BoxDecoration(
          image:
              DecorationImage(image: AssetImage('assets/images/light-2.png'))),
    );
    Widget image3 = Container(
      decoration: const BoxDecoration(
          image:
              DecorationImage(image: AssetImage('assets/images/clock-1.png'))),
    );
    Widget text = Container(
      margin: const EdgeInsets.only(top: 150),
      child: Center(
        child: Text(
          "Login",
          style: TextStyle(
              color:
                  Theme.of(context).textTheme.bodyMedium!.color!.withAlpha(200),
              fontSize: 40,
              fontWeight: FontWeight.bold),
        ),
      ),
    );

    Future<void> forget() async {
      var emailController = TextEditingController();
      var codeController = TextEditingController();
      var newPassController = TextEditingController();
      var newRePassController = TextEditingController();

      await showDialog(
        barrierDismissible:false,
        context: context,
        builder: (context) {
          Future<bool> getCode() async {
            bool aaa = false;
            if (emailController.text.isEmpty) {
              EasyLoading.showError("请输入邮箱");
              return false;
            }
            await Api.request(
              isToast: true,
              data: {"email": emailController.text},
              url: MobileAppUrl.forgetByEmailCode,
              success: (res) {
                aaa = true;
              },
            );
            return aaa;
          }

          return AlertDialog(
            title: const Text('找回密码'),
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                TextField(
                  textInputAction: TextInputAction.next,
                  keyboardType: TextInputType.visiblePassword,
                  controller: emailController,
                  decoration: const InputDecoration(
                    prefixIcon: Icon(Icons.email),
                    hintText: "请输入邮箱地址",
                    labelText: "邮箱",
                  ),
                ),
                CodeInput(
                  codeController: codeController,
                  getCode: () async => await getCode(),
                ),
                TextField(
                  textInputAction: TextInputAction.next,
                  keyboardType: TextInputType.visiblePassword,
                  obscureText: true,
                  controller: newPassController,
                  decoration: const InputDecoration(
                    prefixIcon: Icon(Icons.password),
                    hintText: "请输入新密码",
                    labelText: "新密码",
                  ),
                ),
                TextField(
                  textInputAction: TextInputAction.next,
                  keyboardType: TextInputType.visiblePassword,
                  obscureText: true,
                  controller: newRePassController,
                  decoration: const InputDecoration(
                    prefixIcon: Icon(Icons.password),
                    hintText: "请再次输入新密码",
                    labelText: "新密码",
                  ),
                ),
              ],
            ),
            actions: [
              TextButton(
                child: const Text('取消'),
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
              TextButton(
                child: const Text('确定'),
                onPressed: () async {
                  Api.request(
                    isToast: true,
                    url: MobileAppUrl.forgetByEmail,
                    data: {
                      "email": emailController.text,
                      "emailCode": codeController.text,
                      "password":newPassController.text,
                    },
                    success: (res) {

                      Navigator.pop(context);
                    },
                  );
                },
              ),
            ],
          );
        },
      );
    }


    return Scaffold(
        extendBodyBehindAppBar: true,
        // backgroundColor: Theme.of(context).primaryColor,
        appBar: AppBar(
          title: Text("登录"),
        ),
        body: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              Stack(
                children: <Widget>[
                  Positioned(left: 30, width: 80, height: 200, child: image1),
                  Positioned(left: 140, width: 80, height: 150, child: image2),
                  Positioned(
                      top: 40,
                      right: 40,
                      width: 80,
                      height: 150,
                      child: image3),
                  Positioned(child: text)
                ],
              ),
              Padding(
                padding: const EdgeInsets.all(30.0),
                child: Column(
                  children: <Widget>[
                    Container(
                      padding: const EdgeInsets.all(5),
                      child: Column(
                        children: <Widget>[
                          TextField(
                            textInputAction: TextInputAction.next,
                            controller: accountController,
                            keyboardType: TextInputType.phone,
                            decoration: const InputDecoration(
                              prefixIcon: Icon(Icons.account_box),
                              hintText: "请输入账号",
                              labelText: "账号",
                            ),
                          ),
                          SizedBox(height: 10),
                          TextField(
                            textInputAction: TextInputAction.next,
                            controller: passwordController,
                            keyboardType: TextInputType.visiblePassword,
                            obscureText: true,
                            decoration: const InputDecoration(
                              prefixIcon: Icon(Icons.password),
                              hintText: "请输入密码",
                              labelText: "密码",
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      margin: const EdgeInsets.only(left: 210, top: 2),
                      width: 100,
                      height: 30,
                      child: InkWell(
                        onTap: () async => await forget(),
                        borderRadius: BorderRadius.circular(10),
                        child: Center(
                          child: Text(
                            "忘记密码?",
                            style: TextStyle(
                                color: Theme.of(context)
                                    .textTheme
                                    .bodyMedium!
                                    .color!
                                    .withAlpha(500)),
                          ),
                        ),
                      ),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () => login(),
                      child: const SizedBox(
                        height: 50,
                        child: Center(
                          child: Text(
                            "登录",
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                        ),
                      ),
                    ),
                    const SizedBox(height: 10),
                    InkWell(
                      onTap: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => RegisterPage()),
                        );
                      },
                      borderRadius: BorderRadius.circular(5),
                      child: SizedBox(
                        height: 50,
                        child: Center(
                          child: Text(
                            "前往注册",
                            style: TextStyle(
                                color: Theme.of(context)
                                    .textTheme
                                    .bodyMedium!
                                    .color!
                                    .withAlpha(500),
                                fontWeight: FontWeight.bold),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              )
            ],
          ),
        ));
  }
}
